package com.ruoyi.framework.websocket;

import com.ruoyi.videoHandler.Transmit;
import com.ruoyi.videoHandler.VideoStreamHandler;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * websocket 配置
 *
 * @author ruoyi
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig /*implements WebSocketConfigurer*/ {
    /*@Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(videoStreamHandler(), "/videoStream")
                .setAllowedOriginPatterns("*");
    }

    @Bean
    public VideoStreamHandler videoStreamHandler() {
        return new VideoStreamHandler();
    }*/

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        ServerEndpointExporter exporter = new ServerEndpointExporter();

        // 手动注册 WebSocket 端点
//        exporter.setAnnotatedEndpointClasses(WebSocketServer.class);
        exporter.setAnnotatedEndpointClasses(VideoStreamHandler.class);
        exporter.setAnnotatedEndpointClasses(Transmit.class);
        return exporter;
    }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return factory -> {
            if (factory instanceof TomcatServletWebServerFactory) {
                TomcatServletWebServerFactory tomcatFactory = (TomcatServletWebServerFactory) factory;
                tomcatFactory.addConnectorCustomizers(connector -> {
                    // 设置最大传输限制
                    connector.setMaxSavePostSize(500 * 1024 * 1024); // 50MB
                    connector.setMaxPostSize(500 * 1024 * 1024); // 50MB
                });
            }
        };
    }
}
